package cn.b.sky.mes.mm.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.MoveType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.MoveTypeCondition;
import cn.b.sky.mes.mm.service.MoveTypeService;
import cn.b.sky.mes.mm.service.UserWarehouseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;

import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

@Controller
@RequestMapping(value = "/moveType/*")
public class MoveTypeController extends SecurityController {

  private static final String MOVE_TYPE_LIST_PAGE = "mm/moveType/moveType_list";

  private static final String MOVE_TYPE_PAGE = "mm/moveType/moveTypeInfo";

  @Autowired
  MoveTypeService moveTypeService;

  @Autowired
  ECodeService eCodeService;

  @Autowired
  UserWarehouseService userWarehouseService;

  @RequestMapping("moveType_list")
  public String selectMoveTypeListByCondition(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    return MOVE_TYPE_LIST_PAGE;
  }

  @RequestMapping("selectMoveTypeList")
  @ResponseBody
  public DWZResult selectMoveTypeList(@ModelAttribute("condition") MoveTypeCondition condition) {
    if (StringUtils.isEmpty(condition.getSiteCode())) {
      condition.setSiteCode(getSiteCode());
    }
    condition.setLanguageCode(SecurityContextUtil.getLanguageCode());
    List<MoveType> list = moveTypeService.selectMoveTypeListByCondition(condition);
    int count = moveTypeService.selectMoveTypeCountByCondition(condition);
    condition.setTotalCount(count);
    condition.setData(list);
    return ajaxDone(condition);
  }

  @RequestMapping("addMoveType")
  public String addMoveType(String navTabId, Model model) {
    model.addAttribute("pattern", "add");
    bindModel(null, navTabId, model);
    return MOVE_TYPE_PAGE;
  }

  @RequestMapping("modifyMoveType")
  public String modifyMoveType(String id, String navTabId, Model model) {
    model.addAttribute("pattern", "modify");
    bindModel(id, navTabId, model);
    return MOVE_TYPE_PAGE;
  }

  @RequestMapping("viewMoveType")
  public String viewMoveType(String id, String navTabId, Model model) {
    model.addAttribute("pattern", "view");
    bindModel(id, navTabId, model);
    return MOVE_TYPE_PAGE;
  }

  private void bindModel(String id, String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    List<ECode> eCodes = eCodeService.selectECodeList(ECodeEnums.MOVEDIRECTIONTYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("eCodes", eCodes);
    if (!StringUtils.isEmpty(id)) {
      MoveType moveType = moveTypeService.getMoveType(getSiteCode(), id);
      model.addAttribute("moveType", moveType);
      model.addAttribute("moveTypeUsed", !userWarehouseService.selectUserWarehouseByMoveType(id, getSiteCode())
          .isEmpty());
    }
  }

  @RequestMapping("saveMoveType")
  @ResponseBody
  public DWZResult saveMoveType(String id, MoveType moveType, String navTabId, Model model) {
    if (null == moveType || StringUtils.isBlank(moveType.getMoveTypeCode())) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    moveType.setSiteCode(getSiteCode());
    if (moveType.getLockFlag() == null) {
      moveType.setLockFlag(0);
    }
    setCreateInfo(moveType);
    if (StringUtils.isBlank(id)) {
      moveTypeService.insertMoveType(moveType);
    } else {
      moveTypeService.updateMoveType(moveType);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteMoveType")
  @ResponseBody
  public DWZResult deleteMoveType(String navTabId, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }

    String[] idArr = ids.split(",");
    BatchJobReport[] reports = new BatchJobReport[idArr.length];
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      reports[i] = moveTypeService.deleteMoveType(getSiteCode(), idArr[i]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }

    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("validateMoveTypeCode")
  @ResponseBody
  public boolean validateMoveTypeCode(String moveTypeCode) {
    if (StringUtils.isBlank(moveTypeCode)) {
      return true;
    } else {
      MoveType moveType = moveTypeService.getMoveType(getSiteCode(), moveTypeCode);
      return null == moveType;
    }
  }

  @RequestMapping("moveTypeLookUp")
  public String moveTypeLookUp(Model model, String lookupType,
      @ModelAttribute("condition") MoveTypeCondition condition, String type) {
    condition.setSiteCode(getSiteCode());
    condition.setLanguageCode(SecurityContextUtil.getLanguageCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    List<MoveType> list = moveTypeService.selectMoveTypeListByCondition(condition);
    int count = moveTypeService.selectMoveTypeCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "mm/moveType/moveType_lookup";
  }

  @RequestMapping("moveTypeSuggest")
  @ResponseBody
  public List<MoveType> moveTypeSuggest(@ModelAttribute("condition") MoveTypeCondition condition, String type) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLanguageCode(SecurityContextUtil.getLanguageCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    return moveTypeService.selectMoveTypeSuggests(condition);
  }

}
